<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8" />
        <title>Sign in &laquo; Admin</title>
        <link rel="stylesheet" href="../assets/vendors/bootstrap/css/bootstrap.css" />
        <link rel="stylesheet" href="../assets/css/admin.css" />
    </head>

    <body>
        <div
            style="position:fixed;right:0;top:0;color:white;width:300px;height:50px;background:pink;z-index:999;"
        >
            <div>账号：itheima@itcast.cn</div>
            <div>密码：123456</div>
        </div>
        <div class="login">
            <div class="login-wrap">
                <img class="avatar" src="../assets/img/default.png" />
                <!-- 有错误信息时展示 -->
                <!-- <div class="alert alert-danger">
        <strong>错误！</strong> 用户名或密码错误！
      </div> -->
                <div class="form-group">
                    <label for="email" class="sr-only">邮箱</label>
                    <input
                        id="email"
                        type="email"
                        class="form-control"
                        placeholder="邮箱"
                        autofocus
                    />
                </div>
                <div class="form-group">
                    <label for="password" class="sr-only">密码</label>
                    <input id="password" type="password" class="form-control" placeholder="密码" />
                </div>
                <button class="btn btn-primary btn-block" id="loginBtn">登 录</button>
            </div>
        </div>
        <script src="../assets/vendors/jquery/jquery.min.js"></script>
        <script>
            $('#loginBtn').on('click', function() {
                var email = $('#email').val();
                var password = $('#password').val();
                console.log(email, password);

                if (email.trim().length == 0) {
                    alert('邮箱不能为空');
                    return;
                }
                if (password.trim().length == 0) {
                    alert('密码不能为空');
                    return;
                }
                $.ajax({
                    type: 'post',
                    url: '/login',
                    data: {
                        email: email,
                        password: password
                    },
                    success: function(response) {
                        // 对用户的角色进行判断
                        if (response.role == 'admin') {
                            // 登录成功 跳转到数据管理的首页面
                            location.href = '/admin/index.html';
                        } else {
                            location.href = '/index.html';
                        }
                    },
                    error: function() {
                        // 登录失败
                        alert('邮箱或者密码写错了');
                    }
                });
            });
        </script>
    </body>
</html>
